import scrapy

from schoolInfo.items import SchoolinfoItem

class MinkouSpiderSpider(scrapy.Spider):
    name = 'minkou_spider'
    allowed_domains = ['www.minkou.jp']
    start_urls = ['https://www.minkou.jp/hischool/ranking/deviation/']

    def parse(self, response):
        school_list = response.xpath("//*[@id='main']/div[10]/ul/li[@class='mod-listRanking-list']")
        for i_item in school_list:
            school_item = SchoolinfoItem()
            #获取序号
            serial_number_link = i_item.xpath(".//div[@class='mod-listRanking-name']/a/@href").extract_first()
            #获取序号倒数第二位 正常格式  /hischool/school/3406/
            serial_number = serial_number_link.split('/')[-2]
            school_item['serial_number'] = serial_number
            #学校类型
            school_item['xxlx'] = response.xpath(".//div[@class='mod-listRanking-name']/span/text()").extract_first()
            # 进入详情页面
            # detail_html = scrapy.Request("https://www.minkou.jp" + serial_number_link)
            # detail_link = response.xpath(".//div[@class='mod-listRanking-name']/a/@href").extract()[0]
            detail_url = "https://www.minkou.jp"+serial_number_link
            print(detail_url)
            yield scrapy.Request(url=detail_url, meta = {"school_item":school_item}, callback=self.parse_detail, dont_filter=True)
            # print("detail_html:",detail_html)

            # print(school_item)

    # def detail_page(self,detail_link):
    #     # 进入详情页面
    #     detail_html=scrapy.Request("https://www.minkou.jp" + detail_link)
    #     return detail_html


    def parse_detail(self,response):
        # 创建一个school_item对象
        # school_item = SchoolinfoItem()
        school_item =  response.meta['school_item']
        # 获取序号
        # serial_number = response.meta["serial_number"]
        # school_item['serial_number'] = serial_number

        #获取学校名称
        school_item['name'] = response.xpath("//*[@id='main']/div[1]/div[1]/div/div[1]/h1/text()").extract_first()
        #偏差值
        school_item['pcz'] = response.xpath('//*[@id="main"]/div[1]/div[1]/div/div[2]/div/p[1]/span/text()').extract_first()
        #口碑
        school_item['kb'] = response.xpath('//*[@id="main"]/div[1]/div[1]/div/div[2]/div/p[3]/span/text()').extract_first()
        #概要
        school_item['gy'] = response.xpath('//*[@id="main"]/div[3]/div[1]/div[1]/div/div/dl/dd/p/text()').extract_first()
        school_item['xxcsmr'] = response.xpath('//*[@id="main"]/div[3]/div[1]/div[2]/p/text()').extract_first()

        # school_item['zhpj'] = response.xpath(".//div[@class='mod-listRanking-name']/a/text()").extract_first()


        # print(school_item)
        yield school_item

#参照-------
    # def parse_main_page(self, response):
    #     item = MyItem()
    #     item['main_url'] = response.url
    #
    #     url1 = response.xpath('//a[@class="link1"]/@href').extract()[0]
    #     request1 = scrapy.Request(url1, callback=self.parse_page1)
    #
    #     url2 = response.xpath('//a[@class="link2"]/@href').extract()[0]
    #     request2 = scrapy.Request(url2, callback=self.parse_page2)
    #
    #     url3 = response.xpath('//a[@class="link3"]/@href').extract()[0]
    #     request3 = scrapy.Request(url3, callback=self.parse_page3)
    #
    #     request.meta['item'] = item
    #     request.meta['requests'] = [request2, request3]
    #     return request1
    #
    # def parse_page1(self, response):
    #     item = response.meta['item']
    #     item['data1'] = response.xpath('//div[@class="data1"]/text()').extract()[0]
    #
    #     return request.meta['requests'].pop(0)
    #
    # def parse_page2(self, response):
    #     item = response.meta['item']
    #     item['data2'] = response.xpath('//div[@class="data2"]/text()').extract()[0]
    #
    #     return request.meta['requests'].pop(0)
    #
    # def parse_page3(self, response):
    #     item = response.meta['item']
    #     item['data3'] = response.xpath('//div[@class="data3"]/text()').extract()[0]
    #
    #     return item